<template>
<view>
<!-- 2 辅助线 -->

<view v-if="PaiXuPartials" class="container">
  <block>
	  <custom-page-index-component id="container" class="aaa" :receiveData="partialsName" :authorizationState="showAuthorizationPopup" v-if="partialsName"></custom-page-index-component>
  </block>
  <component-footer></component-footer>
</view>   
<view v-else class="blankPage">
   <view class="blankPage_image"></view>
   <text>网络异常，请刷新本页</text>
   <button></button>
</view>
</view>
</template>

<script>
// var util = require("../../wxcomponents/utils/util.js");
var timer; // 计时器
function Countdown(page, that) {
  console.log('2');

  if (page.loginUser) {
    that.setData({
      loginUser: page.loginUser
    });
  } else {
    timer = setTimeout(function () {
      Countdown(page, that);
    }, 500);
  }
}

export default {
  data() {
    return {
      listenerId: null,
      state:true,
      /* seeting */
      setting: null,
      renderData: null,
      PaiXuPartials: [],
      partialsName: '',
      loginUser: null,
      showAuthorizationPopup: false,
      sysWidth: "",
      showBannerState:true,
    };
  },
  props: {},

  /*onload*/
  onLoad: function (options) {
    console.warn("======onLoad:options======", options);
    if (!getApp().globalData.setting) {
      console.warn("======!getApp().globalData.setting======");
      getApp().globalData.promiseonLaunch(this);
    } else {
      console.warn("======getApp().globalData.setting======");
      this.setData({
        sysWidth: getApp().globalData.sysWidth,
        setting: getApp().globalData.setting
      });
      let partialsName = JSON.stringify({
        url: "index",
        state: this.showAuthorizationPopup
      });
      this.setData({
        partialsName: partialsName
      });
	  console.log("=====partialsName====",partialsName,this.partialsName)
      if (!!getApp().globalData.setting) {
        this.setNavBar();
      }
    }
  },
  onReady: function () {
	  console.log("===onReady=====")
    if (getApp().globalData.shareParam && getApp().globalData.shareParam.pageName) {
      console.log("这是custom_page里面ready事件的shareParam" + getApp().globalData.shareParam);
      this.jumpToPage(getApp().globalData.shareParam);
    } // Countdown(app,this);

  },
  onShow: function () {
	console.log("====onShow=====");
    let that=this;
    getApp().globalData.addAuthorizationListenerItem(this)
    that.showBannerState=true;
    if(that.selectComponent("#container")){
        that.showBannerState=true;
        that.selectComponent("#container").setShowBannerStateFun(true);
    }
    // this.audioCtx = wx.createAudioContext('myAudio');
    // let Time2 = util.formatTime(new Date()); //当前时间

    // let OldTime = '2018-3-1 15:20:33';
    // let result = util.GetDateDiff(OldTime, Time2, 'second');
    // let sss = util.timeStamp(result);
    // console.log(sss);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
      let that=this;
      that.showBannerState=false;
      that.selectComponent("#container").setShowBannerStateFun(false);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
      let that=this;
      that.showBannerState=false;
      that.selectComponent("#container").setShowBannerStateFun(false);
  },

  /* 分享 app.js862行*/
  onShareAppMessage: function () {
    console.log(getApp().globalData.miniIndexPage);
    return getApp().globalData.shareForFx2(getApp().globalData.miniIndexPage);
  },
  onPullDownRefresh: function () {
    let that = this; // 下拉刷新的时候首先判断存不存在tab
    console.log("下拉");
    that.refreshPage();
    wx.stopPullDownRefresh();
  },
  methods: {
      show:function(){
          let that=this;
          that.setData({state:true})
      },
      close:function(){
          let that=this;
          that.setData({state:false})
      },
    setNavBar: function () {
      if (getApp().globalData.setting.platformSetting&&getApp().globalData.setting.platformSetting.siteTitle == '') {
        wx.setNavigationBarTitle({
          title: '首页'
        });
      } else {
        wx.setNavigationBarTitle({
          title: getApp().globalData.setting.platformSetting?getApp().globalData.setting.platformSetting.siteTitle:'默认首页'
        });
      }

      wx.setNavigationBarColor({
        frontColor: getApp().globalData.setting.platformSetting?getApp().globalData.setting.platformSetting.topColor.toLowerCase():'',
        backgroundColor: getApp().globalData.setting.platformSetting?getApp().globalData.setting.platformSetting.topBgColor:''
      });
    },
    getStateData: function (state) {
      let that = this;
      console.log("===getStateData===", state, that.showAuthorizationPopup);
      that.selectComponent("#container").setPopupState(state);
    },
    jumpToPage: function (shareParam) {
      if (shareParam.pageName == 'custom_page_index') {
        return;
      } else if (shareParam.pageName == 'shopping_car_list') {
        return;
      } else if (shareParam.pageName == 'custom_page_userinfo') {
        return;
      } else {
        let paramStr = getApp().globalData.jsonToStr(shareParam);
        wx.navigateTo({
          url: '/pages/' + shareParam.pageName + '/index' + paramStr
        });
      }
    },
    onPageScroll: function (object) {
	  console.log("====onPageScroll====")
      try{
          var positionNotifyer = this.selectComponent("#container").selectAllComponents(".positionNotifyer");
          if (positionNotifyer != null) {
            for (let i = 0; i < positionNotifyer.length; i++) {
              try {
                positionNotifyer[i].scrollTo(object);
              } catch (e) {}
            }
          }
      }catch(e){
          //TODO handle the exception
      }
    },

    /* 组件事件集合 */
    tolinkUrl: function (e) {
      console.warn("=======e=======", e);
      let linkUrl = e.currentTarget.dataset.link;
      getApp().globalData.linkEvent(linkUrl);
    },
    setPageName: function (urlStr) {
      let url = urlStr || "";
      let partialsName = "";

      if (url) {
        partialsName = JSON.stringify({
          url: url,
          state: this.showAuthorizationPopup
        });
      }

      this.setData({
        partialsName: partialsName
      });
      console.log("partialsName", this.partialsName);
    },
    refreshPage: function () {
      let that = this;
      console.log("首页刷新");
      that.setPageName();
      wx.showToast({
        title: '加载中...',
        icon: 'none',
        duration: 500
      });
      setTimeout(function () {
        that.setPageName('index');
        getApp().globalData.footerCount = 0;
      }, 500);
    },
    setData: function (obj) {
      let that = this;
      let keys = [];
      let val, data;
      Object.keys(obj).forEach(function (key) {
        keys = key.split('.');
        val = obj[key];
        data = that.$data;
        keys.forEach(function (key2, index) {
          if (index + 1 == keys.length) {
            that.$set(data, key2, val);
          } else {
            if (!data[key2]) {
              that.$set(data, key2, {});
            }
          }

          data = data[key2];
        });
      });
    }
  }
};
</script>
<style>
@import "./index.css";
</style>